package com.dell.chen.servlet;

import com.dell.chen.pojo.Admin;
import com.dell.chen.service.AdminService;
import com.dell.chen.service.Impl.AdminServiceImpl;
import com.dell.chen.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ：ChenwithIU
 * @date ：Created in 2020/11/28 9:51
 */
public class AdminServlet extends BaseServlet {

    private AdminService adminService = new AdminServiceImpl();

    protected void adminLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        Admin admin = adminService.login(new Admin(username,password));

        if (admin == null){
            //登录失败
            req.setAttribute("msg","用户名密码错误!");
            req.setAttribute("username",username);
            req.setAttribute("password",password);

            req.getRequestDispatcher("manager/manager_login.jsp").forward(req,resp);
        }else {
            //非空，登录成功：
            req.getSession().setAttribute("admin",admin);

            req.getRequestDispatcher("manager/manager.jsp").forward(req,resp);
        }

    }

    protected void updateAdmin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String oldPassword = req.getParameter("oldpassword");
        String password = req.getParameter("password");
        String password2 = req.getParameter("password2");

        if (password.equals(password2)){
            //两次密码输入一致：
            adminService.update(password,oldPassword);

            req.setAttribute("msg","修改密码成功!");
            //返回登录界面重新登录

        }else {
            req.setAttribute("msg","两次密码不一致!");
            //不跳转，当前页面
        }

    }



}
